﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.BlackApart.Data;

namespace blackhouse.BlackApart.Business.ApartmentServices {
    public class DaReservationAdditionalService : ReservationAdditionalService {

        #region Fields

        private IAdditionalServiceDataAccess dataAccess;
        private int orderId = -1;
        private string clientReview;

        #endregion

        #region Properties

        private int OrderId {
            get {
                if (orderId == -1)
                    this.SetOrder();
                return this.orderId;
            }
            set {
                this.orderId = value;
            }
        }

        private string InClientReview {
            get {
                if (orderId == -1)
                    this.SetOrder();
                return this.clientReview;
            }
            set {
                this.clientReview = value;
            }
        }

        #endregion

        #region Constuctors

        public DaReservationAdditionalService(IWebReservation reservation, IAdditionalService service, IAdditionalServiceDataAccess dataAccess)
            : base(reservation, service) {
            this.dataAccess = dataAccess;
        }

        #endregion

        #region Methods

        private void SetOrder() {
            ReservationAdditionalServiceData data = this.dataAccess.GetReservationService(reservation.ReservationId, this.Service.ServiceId);
            this.OrderId = data.OrderId;
            this.InClientReview = data.ClientReview;
        }

        protected override void SaveOrder() {
            ReservationServiceSaveTransaction transaction = Factory.Instance().GetDataFactory().GetReservationServiceSaveTransaction();
            transaction.SetReservationServiceSaveTransaction(this.reservation.ReservationId, this.Service.ServiceId, this.Service.UnitPrice, this.ClientReview, (int)this.Service.ServiceFor);

            foreach (ReservationAdditionalServiceUnit item in this.Lines) {
                transaction.AddLine(item.Date, item.Quantity, item.ApartmentId);
            }
            try {
                this.OrderId = transaction.Save();
            }
            catch (Exception e) {
                throw new Exception("Błąd przy zapisie usługi", e);
            }
        }

        protected override string GetClientReview() {
            return this.InClientReview;
        }

        protected override List<ReservationAdditionalServiceUnit> GetOrderLines() {
            if (this.OrderId > 0) {
                ReservationAdditionalServiceUnitData[] data = this.dataAccess.GetServiceItemFromOrder(this.OrderId);
                List<ReservationAdditionalServiceUnit> lines = new List<ReservationAdditionalServiceUnit>(data.Length);
                foreach (ReservationAdditionalServiceUnitData dataItem in data) {
                    ReservationAdditionalServiceUnit line = new ReservationAdditionalServiceUnit();
                    line.Date = dataItem.Date;
                    line.UnitPrice = dataItem.UnitPrice;
                    line.Quantity = dataItem.Quantity;
                    line.ApartmentId = dataItem.ApartmentId;
                    lines.Add(line);
                }
                return lines;
            }
            else
                return null;
        }

        #endregion

    }
}
